package com.shopping.miaosha.exceptionHander;

import com.shopping.miaosha.error.ErrorCode;
import com.shopping.miaosha.response.ResultType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * @author canger
 * @Deseription 全局异常对象
 * @create 2019/3/25 18:09
 **/
@Component
@RestControllerAdvice
public class GloadExceptionHander {

    @ExceptionHandler(Exception.class)
    public ResultType getHandlerException(HttpServletRequest request, Exception ex){
        Map<Object, Object> exceptionData = new HashMap<>();
        if (ex instanceof BusinessException) {
            BusinessException businessException= (BusinessException)ex;
            exceptionData.put("errCode",businessException.getCode());
            exceptionData.put("errMsg",businessException.getMsg());
        } else {
            exceptionData.put("errCode", ErrorCode.UNKNOW_ERROR.getCode());
            exceptionData.put("errMsg",ErrorCode.UNKNOW_ERROR.getMsg());
        }
        return ResultType.getResult(exceptionData,"fail");

    }

}
